{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:41.899281Z",
     "start_time": "2024-09-21T02:31:41.882089Z"
    }
   },
   "source": [
    "# 数据处理、数据评分相关库\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "# 画图\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "%matplotlib inline\n",
    "plt.rcParams['font.sans-serif']='SimHei'# 设置中文显示\n",
    "plt.rcParams['font.size']=14 # 设置字体大小\n",
    "matplotlib.rcParams['axes.unicode_minus'] = False # 解决负号问题\n",
    "\n",
    "#忽略警号\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.172622Z",
     "start_time": "2024-09-21T02:31:41.900282Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# user_log = pd.read_csv('../data/data_format1/sample_user_log_format1.csv',index_col='Unnamed: 0')# 少量样本数据，跑通模型\n",
    "user_log = pd.read_csv(r'D:\\桌面\\天猫复购预测\\data\\data_format1\\user_log_format1.csv')\n",
    "user_info = pd.read_csv(r'D:\\桌面\\天猫复购预测\\data\\data_format1\\user_info_format1.csv')\n",
    "train = pd.read_csv(r'D:\\桌面\\天猫复购预测\\data\\data_format1\\train_format1.csv')\n",
    "test = pd.read_csv(r'D:\\桌面\\天猫复购预测\\data\\data_format1\\test_format1.csv')"
   ],
   "id": "7f9fa1b71a4c226c",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.178973Z",
     "start_time": "2024-09-21T02:31:59.173622Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train['origin'] = 'train'\n",
    "test['origin'] = 'test'"
   ],
   "id": "7ee803ac4dc5c79c",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.190278Z",
     "start_time": "2024-09-21T02:31:59.179973Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 连接train、test表\n",
    "data = pd.concat([train, test], ignore_index=True, sort=False)"
   ],
   "id": "1bd5129b325e349d",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.285212Z",
     "start_time": "2024-09-21T02:31:59.191278Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 连接user_info表\n",
    "data = pd.merge(user_info,data,on='user_id',how='inner')"
   ],
   "id": "f31120b537af6f36",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.298200Z",
     "start_time": "2024-09-21T02:31:59.286214Z"
    }
   },
   "cell_type": "code",
   "source": "data.drop(['prob'],axis=1,inplace=True)",
   "id": "5f75eda3954777d7",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.323629Z",
     "start_time": "2024-09-21T02:31:59.299203Z"
    }
   },
   "cell_type": "code",
   "source": "data.info()",
   "id": "334d87dd049bca6b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 522341 entries, 0 to 522340\n",
      "Data columns (total 6 columns):\n",
      " #   Column       Non-Null Count   Dtype  \n",
      "---  ------       --------------   -----  \n",
      " 0   user_id      522341 non-null  int64  \n",
      " 1   age_range    519763 non-null  float64\n",
      " 2   gender       514796 non-null  float64\n",
      " 3   merchant_id  522341 non-null  int64  \n",
      " 4   label        260864 non-null  float64\n",
      " 5   origin       522341 non-null  object \n",
      "dtypes: float64(3), int64(2), object(1)\n",
      "memory usage: 23.9+ MB\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.331052Z",
     "start_time": "2024-09-21T02:31:59.324629Z"
    }
   },
   "cell_type": "code",
   "source": "train['label'].value_counts()/len(train)# 样本不均衡，需要处理",
   "id": "7b87e195cb725b3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "label\n",
       "0    0.938849\n",
       "1    0.061151\n",
       "Name: count, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.334521Z",
     "start_time": "2024-09-21T02:31:59.332052Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 整理user_log表\n",
    "user_log.rename(columns={'seller_id':'merchant_id'},inplace=True)"
   ],
   "id": "a765e2710721de76",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.339896Z",
     "start_time": "2024-09-21T02:31:59.334521Z"
    }
   },
   "cell_type": "code",
   "source": "user_log.info()",
   "id": "cbc6536dad3c252e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   merchant_id  int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   int64  \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(6)\n",
      "memory usage: 2.9 GB\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "数据预处理",
   "id": "a67289e6b9e9dee9"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "压缩数据¶",
   "id": "f19b19f22c8188d3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "data",
   "id": "e7e472ed3b70ac9c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.344375Z",
     "start_time": "2024-09-21T02:31:59.340894Z"
    }
   },
   "cell_type": "code",
   "source": "data['user_id'] = data['user_id'].astype('int32')",
   "id": "a5d127268c7d45bb",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.349993Z",
     "start_time": "2024-09-21T02:31:59.344375Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data['age_range'].fillna(0,inplace=True)# 0和NULL表示未知\n",
    "data['age_range'] = data['age_range'].astype('int8')"
   ],
   "id": "c525a7cb5a89dde9",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.355526Z",
     "start_time": "2024-09-21T02:31:59.350992Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data['gender'].fillna(2,inplace=True)#2和NULL表示未知\n",
    "data['gender'] = data['gender'].astype('int8')"
   ],
   "id": "4c49f3d60c8cc742",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.360054Z",
     "start_time": "2024-09-21T02:31:59.356528Z"
    }
   },
   "cell_type": "code",
   "source": "data['merchant_id'] = data['merchant_id'].astype('int32')",
   "id": "ba85ef14b49ed87d",
   "outputs": [],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.574705Z",
     "start_time": "2024-09-21T02:31:59.361055Z"
    }
   },
   "cell_type": "code",
   "source": "data['label'] = data['label'].astype('str') ",
   "id": "ff618b897386b61e",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "user_log",
   "id": "c6cf15ad36d71f9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:31:59.967906Z",
     "start_time": "2024-09-21T02:31:59.577986Z"
    }
   },
   "cell_type": "code",
   "source": [
    "user_log['user_id'] = user_log['user_id'].astype('int32')\n",
    "user_log['item_id'] = user_log['item_id'].astype('int32')\n",
    "user_log['cat_id'] = user_log['cat_id'].astype('int32')\n",
    "user_log['merchant_id'] = user_log['merchant_id'].astype('int32')"
   ],
   "id": "1642a71f240bc59e",
   "outputs": [],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:32:00.209685Z",
     "start_time": "2024-09-21T02:31:59.968906Z"
    }
   },
   "cell_type": "code",
   "source": [
    "user_log['brand_id'].fillna(0,inplace=True)\n",
    "user_log['brand_id'] = user_log['brand_id'].astype('int32')"
   ],
   "id": "666f21d7a0859936",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "bbeb14edc18a3346"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:32:27.509224Z",
     "start_time": "2024-09-21T02:32:00.210686Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 添加一个临时年\n",
    "user_log['time_stamp'] = user_log['time_stamp'].astype('str').apply(lambda x:'2020'+x)\n",
    "user_log['time_stamp'] = pd.to_datetime(user_log['time_stamp'],format='%Y%m%d')"
   ],
   "id": "b3115cc61a609250",
   "outputs": [],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:32:27.655546Z",
     "start_time": "2024-09-21T02:32:27.510237Z"
    }
   },
   "cell_type": "code",
   "source": "user_log['action_type'] = user_log['action_type'].astype('int8')",
   "id": "5d52e639792ce26e",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:32:27.661564Z",
     "start_time": "2024-09-21T02:32:27.656546Z"
    }
   },
   "cell_type": "code",
   "source": "user_log.info()",
   "id": "ecaf59f2b839b1fc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype         \n",
      "---  ------       -----         \n",
      " 0   user_id      int32         \n",
      " 1   item_id      int32         \n",
      " 2   cat_id       int32         \n",
      " 3   merchant_id  int32         \n",
      " 4   brand_id     int32         \n",
      " 5   time_stamp   datetime64[ns]\n",
      " 6   action_type  int8          \n",
      "dtypes: datetime64[ns](1), int32(5), int8(1)\n",
      "memory usage: 1.5 GB\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "特征处理",
   "id": "9b3fca1141e12e7"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "User特征处理",
   "id": "d41ca64804dcb501"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:32:27.664693Z",
     "start_time": "2024-09-21T02:32:27.661564Z"
    }
   },
   "cell_type": "code",
   "source": "groups = user_log.groupby(['user_id'])",
   "id": "86e98e40d0adcf6e",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:32:28.527370Z",
     "start_time": "2024-09-21T02:32:27.664693Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计交互行为数量 每个 user_id 中的记录数量 \n",
    "temp = groups.size().reset_index().rename(columns={0:'u1'})\n",
    "\n",
    "temp"
   ],
   "id": "7ef89833ebb8d0b2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        user_id   u1\n",
       "0             1   33\n",
       "1             2   63\n",
       "2             3   68\n",
       "3             4   50\n",
       "4             5  173\n",
       "...         ...  ...\n",
       "424165   424166   90\n",
       "424166   424167   35\n",
       "424167   424168  223\n",
       "424168   424169  297\n",
       "424169   424170   40\n",
       "\n",
       "[424170 rows x 2 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>u1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424165</th>\n",
       "      <td>424166</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424166</th>\n",
       "      <td>424167</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424167</th>\n",
       "      <td>424168</td>\n",
       "      <td>223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424168</th>\n",
       "      <td>424169</td>\n",
       "      <td>297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424169</th>\n",
       "      <td>424170</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>424170 rows × 2 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:32:28.601712Z",
     "start_time": "2024-09-21T02:32:28.528372Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on='user_id',how='left')",
   "id": "26b79b527751da24",
   "outputs": [],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:34:36.599265Z",
     "start_time": "2024-09-21T02:34:20.783103Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计'item_id','cat_id','merchant_id','brand_id' 不重复值个数\n",
    "\n",
    "temp = groups[['item_id', 'cat_id', 'merchant_id', 'brand_id']].nunique().reset_index().rename(columns={\n",
    "    'item_id': 'u2', \n",
    "    'cat_id': 'u3', \n",
    "    'merchant_id': 'u4', \n",
    "    'brand_id': 'u5'\n",
    "})\n"
   ],
   "id": "74898d9490dfbecd",
   "outputs": [],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:34:57.557602Z",
     "start_time": "2024-09-21T02:34:57.490224Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on='user_id',how='left')",
   "id": "5f22ea62d92a3970",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:34:58.280519Z",
     "start_time": "2024-09-21T02:34:57.738001Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计时间间隔\n",
    "temp = groups['time_stamp'].agg([('buy_far_time','min'),('buy_late_time','max')]).reset_index()\n",
    "temp['u6'] = (temp['buy_late_time'] - temp['buy_far_time']).dt.days\n",
    "temp"
   ],
   "id": "b448ffaf0a8aa519",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        user_id buy_far_time buy_late_time   u6\n",
       "0             1   2020-10-09    2020-11-11   33\n",
       "1             2   2020-05-27    2020-11-11  168\n",
       "2             3   2020-05-16    2020-11-11  179\n",
       "3             4   2020-05-27    2020-11-11  168\n",
       "4             5   2020-05-19    2020-11-11  176\n",
       "...         ...          ...           ...  ...\n",
       "424165   424166   2020-05-14    2020-11-11  181\n",
       "424166   424167   2020-05-29    2020-11-11  166\n",
       "424167   424168   2020-05-23    2020-11-11  172\n",
       "424168   424169   2020-05-11    2020-11-11  184\n",
       "424169   424170   2020-11-05    2020-11-11    6\n",
       "\n",
       "[424170 rows x 4 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>buy_far_time</th>\n",
       "      <th>buy_late_time</th>\n",
       "      <th>u6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2020-10-09</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2020-05-27</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2020-05-16</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2020-05-27</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2020-05-19</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424165</th>\n",
       "      <td>424166</td>\n",
       "      <td>2020-05-14</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424166</th>\n",
       "      <td>424167</td>\n",
       "      <td>2020-05-29</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424167</th>\n",
       "      <td>424168</td>\n",
       "      <td>2020-05-23</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424168</th>\n",
       "      <td>424169</td>\n",
       "      <td>2020-05-11</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424169</th>\n",
       "      <td>424170</td>\n",
       "      <td>2020-11-05</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>424170 rows × 4 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:34:58.363429Z",
     "start_time": "2024-09-21T02:34:58.281517Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp[['user_id','u6']],on='user_id',how='left')",
   "id": "b1768d0792945ae3",
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:00.939493Z",
     "start_time": "2024-09-21T02:34:58.364432Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计操作类型为0，1，2，3的个数                 unstack将计数结果转换为宽格式，即将每个操作类型的计数结果转换为一个单独的列。\n",
    "temp = groups['action_type'].value_counts().unstack().reset_index().rename(columns={0:'u7', 1:'u8', 2:'u9', 3:'u10'})\n",
    "\n",
    "temp"
   ],
   "id": "cb514984c5e8a323",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "action_type  user_id     u7  u8    u9   u10\n",
       "0                  1   27.0 NaN   6.0   NaN\n",
       "1                  2   47.0 NaN  14.0   2.0\n",
       "2                  3   63.0 NaN   4.0   1.0\n",
       "3                  4   49.0 NaN   1.0   NaN\n",
       "4                  5  150.0 NaN  13.0  10.0\n",
       "...              ...    ...  ..   ...   ...\n",
       "424165        424166   79.0 NaN  11.0   NaN\n",
       "424166        424167   28.0 NaN   6.0   1.0\n",
       "424167        424168  216.0 NaN   6.0   1.0\n",
       "424168        424169  277.0 NaN  17.0   3.0\n",
       "424169        424170   39.0 NaN   1.0   NaN\n",
       "\n",
       "[424170 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>action_type</th>\n",
       "      <th>user_id</th>\n",
       "      <th>u7</th>\n",
       "      <th>u8</th>\n",
       "      <th>u9</th>\n",
       "      <th>u10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>27.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>47.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>63.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>49.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>150.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424165</th>\n",
       "      <td>424166</td>\n",
       "      <td>79.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424166</th>\n",
       "      <td>424167</td>\n",
       "      <td>28.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424167</th>\n",
       "      <td>424168</td>\n",
       "      <td>216.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424168</th>\n",
       "      <td>424169</td>\n",
       "      <td>277.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>424169</th>\n",
       "      <td>424170</td>\n",
       "      <td>39.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>424170 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:01.051521Z",
     "start_time": "2024-09-21T02:35:00.940494Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp[['user_id', 'u7','u8','u9','u10']],on='user_id',how='left')",
   "id": "3e5142533bdbe47e",
   "outputs": [],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "商家特征处理",
   "id": "5b1a2fd3cf300f14"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:01.055826Z",
     "start_time": "2024-09-21T02:35:01.052522Z"
    }
   },
   "cell_type": "code",
   "source": "groups = user_log.groupby(['merchant_id'])",
   "id": "533e5d545b72727",
   "outputs": [],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:01.811014Z",
     "start_time": "2024-09-21T02:35:01.056826Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计 商家被交互行为数量\n",
    "temp = groups.size().reset_index().rename(columns={0:'m1'})\n",
    "\n",
    "temp"
   ],
   "id": "73825070525ab95b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      merchant_id      m1\n",
       "0               1  339140\n",
       "1               2    2371\n",
       "2               3    2645\n",
       "3               4    3106\n",
       "4               5    8192\n",
       "...           ...     ...\n",
       "4990         4991     666\n",
       "4991         4992   13876\n",
       "4992         4993   14267\n",
       "4993         4994    6159\n",
       "4994         4995    7508\n",
       "\n",
       "[4995 rows x 2 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>m1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>339140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2371</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2645</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>3106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>8192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4990</th>\n",
       "      <td>4991</td>\n",
       "      <td>666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4991</th>\n",
       "      <td>4992</td>\n",
       "      <td>13876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4992</th>\n",
       "      <td>4993</td>\n",
       "      <td>14267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4993</th>\n",
       "      <td>4994</td>\n",
       "      <td>6159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4994</th>\n",
       "      <td>4995</td>\n",
       "      <td>7508</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4995 rows × 2 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:01.864503Z",
     "start_time": "2024-09-21T02:35:01.812014Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on='merchant_id',how='left')",
   "id": "fe4eaece1766388a",
   "outputs": [],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:27.827427Z",
     "start_time": "2024-09-21T02:35:20.272472Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计'user_id','item_id','cat_id','brand_id' 不重复值个数\n",
    "temp = groups[['user_id','item_id','cat_id','brand_id']].nunique().reset_index().rename(columns={\n",
    "    'user_id':'m2','item_id':'m3','cat_id':'m4','brand_id':'m5'})\n",
    "\n",
    "temp"
   ],
   "id": "a1f986af614b9a88",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      merchant_id     m2    m3  m4  m5\n",
       "0               1  30796  2977  44   3\n",
       "1               2    936   154  10   2\n",
       "2               3   1136   171   4   2\n",
       "3               4   1481   155   7   3\n",
       "4               5   3652   660  23   2\n",
       "...           ...    ...   ...  ..  ..\n",
       "4990         4991    227    32   2   2\n",
       "4991         4992   2570    98   2   2\n",
       "4992         4993   3986   162   2   2\n",
       "4993         4994   2736   778  15   4\n",
       "4994         4995   3017   288  84  16\n",
       "\n",
       "[4995 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>m2</th>\n",
       "      <th>m3</th>\n",
       "      <th>m4</th>\n",
       "      <th>m5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>30796</td>\n",
       "      <td>2977</td>\n",
       "      <td>44</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>936</td>\n",
       "      <td>154</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1136</td>\n",
       "      <td>171</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1481</td>\n",
       "      <td>155</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>3652</td>\n",
       "      <td>660</td>\n",
       "      <td>23</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4990</th>\n",
       "      <td>4991</td>\n",
       "      <td>227</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4991</th>\n",
       "      <td>4992</td>\n",
       "      <td>2570</td>\n",
       "      <td>98</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4992</th>\n",
       "      <td>4993</td>\n",
       "      <td>3986</td>\n",
       "      <td>162</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4993</th>\n",
       "      <td>4994</td>\n",
       "      <td>2736</td>\n",
       "      <td>778</td>\n",
       "      <td>15</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4994</th>\n",
       "      <td>4995</td>\n",
       "      <td>3017</td>\n",
       "      <td>288</td>\n",
       "      <td>84</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4995 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:35.947988Z",
     "start_time": "2024-09-21T02:35:35.885518Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on='merchant_id',how='left')",
   "id": "9a182d25f79b2950",
   "outputs": [],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:37.953504Z",
     "start_time": "2024-09-21T02:35:36.150322Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计商家被交互的action_type数量\n",
    "temp = groups['action_type'].value_counts().unstack().reset_index().rename(columns={0:'m6', 1:'m7', 2:'m8', 3:'m9'})\n",
    "\n",
    "temp"
   ],
   "id": "ae6ec0b57e7d67df",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "action_type  merchant_id        m6     m7       m8       m9\n",
       "0                      1  308236.0  444.0  17705.0  12755.0\n",
       "1                      2    2030.0    8.0    189.0    144.0\n",
       "2                      3    2399.0    4.0     67.0    175.0\n",
       "3                      4    2646.0    2.0    294.0    164.0\n",
       "4                      5    7483.0    9.0    144.0    556.0\n",
       "...                  ...       ...    ...      ...      ...\n",
       "4990                4991     556.0    2.0     80.0     28.0\n",
       "4991                4992   11380.0   20.0   1971.0    505.0\n",
       "4992                4993   12324.0   18.0    769.0   1156.0\n",
       "4993                4994    5756.0   13.0    164.0    226.0\n",
       "4994                4995    6134.0   16.0    911.0    447.0\n",
       "\n",
       "[4995 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>action_type</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>m6</th>\n",
       "      <th>m7</th>\n",
       "      <th>m8</th>\n",
       "      <th>m9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>308236.0</td>\n",
       "      <td>444.0</td>\n",
       "      <td>17705.0</td>\n",
       "      <td>12755.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2030.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>189.0</td>\n",
       "      <td>144.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2399.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>175.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2646.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>294.0</td>\n",
       "      <td>164.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>7483.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>556.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4990</th>\n",
       "      <td>4991</td>\n",
       "      <td>556.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>28.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4991</th>\n",
       "      <td>4992</td>\n",
       "      <td>11380.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>1971.0</td>\n",
       "      <td>505.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4992</th>\n",
       "      <td>4993</td>\n",
       "      <td>12324.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>769.0</td>\n",
       "      <td>1156.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4993</th>\n",
       "      <td>4994</td>\n",
       "      <td>5756.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>226.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4994</th>\n",
       "      <td>4995</td>\n",
       "      <td>6134.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>911.0</td>\n",
       "      <td>447.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4995 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:38.039367Z",
     "start_time": "2024-09-21T02:35:37.955502Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on='merchant_id',how='left')",
   "id": "28d9b8d439a96ccf",
   "outputs": [],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:38.059276Z",
     "start_time": "2024-09-21T02:35:38.040387Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 按照merchant_id统计随机负采样的个数\n",
    "temp = train[train['label']==0].groupby(['merchant_id']).size().reset_index().rename(columns={0:'m10'})\n",
    "\n",
    "temp"
   ],
   "id": "9835d003a47a0df8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      merchant_id  m10\n",
       "0               2   57\n",
       "1               8   21\n",
       "2               9   30\n",
       "3              10  440\n",
       "4              13  166\n",
       "...           ...  ...\n",
       "1988         4987   36\n",
       "1989         4988   22\n",
       "1990         4991   35\n",
       "1991         4992  413\n",
       "1992         4993  127\n",
       "\n",
       "[1993 rows x 2 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>m10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1988</th>\n",
       "      <td>4987</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1989</th>\n",
       "      <td>4988</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990</th>\n",
       "      <td>4991</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1991</th>\n",
       "      <td>4992</td>\n",
       "      <td>413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1992</th>\n",
       "      <td>4993</td>\n",
       "      <td>127</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1993 rows × 2 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:38.138356Z",
     "start_time": "2024-09-21T02:35:38.060276Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on='merchant_id',how='left')",
   "id": "67310846fd94e559",
   "outputs": [],
   "execution_count": 40
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "按照user_id, merchant_id分组",
   "id": "9ccef355bf84a057"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:38.143176Z",
     "start_time": "2024-09-21T02:35:38.140353Z"
    }
   },
   "cell_type": "code",
   "source": "groups = user_log.groupby(['user_id','merchant_id'])",
   "id": "b63c8fafb602e4f6",
   "outputs": [],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:44.908601Z",
     "start_time": "2024-09-21T02:35:38.144181Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计交互行为数量\n",
    "temp = groups.size().reset_index().rename(columns={0:'um1'})\n",
    "\n",
    "temp"
   ],
   "id": "c813404b1ea1d8dd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          user_id  merchant_id  um1\n",
       "0               1          471    1\n",
       "1               1          739    1\n",
       "2               1          925    4\n",
       "3               1         1019   14\n",
       "4               1         1156    1\n",
       "...           ...          ...  ...\n",
       "14058661   424170         1082    1\n",
       "14058662   424170         3469    1\n",
       "14058663   424170         3736   10\n",
       "14058664   424170         4268   25\n",
       "14058665   424170         4963    1\n",
       "\n",
       "[14058666 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>um1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>471</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>739</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>925</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1019</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1156</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058661</th>\n",
       "      <td>424170</td>\n",
       "      <td>1082</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058662</th>\n",
       "      <td>424170</td>\n",
       "      <td>3469</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058663</th>\n",
       "      <td>424170</td>\n",
       "      <td>3736</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058664</th>\n",
       "      <td>424170</td>\n",
       "      <td>4268</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058665</th>\n",
       "      <td>424170</td>\n",
       "      <td>4963</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14058666 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:35:47.777622Z",
     "start_time": "2024-09-21T02:35:44.910597Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on=['user_id','merchant_id'],how='left')",
   "id": "aaa23470b67dc57f",
   "outputs": [],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:11.350464Z",
     "start_time": "2024-09-21T02:39:55.082733Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计'item_id','cat_id','brand_id' 不重复值个数\n",
    "temp = groups[['item_id','cat_id','brand_id']].nunique().reset_index().rename(columns={\n",
    "    'item_id':'um2','cat_id':'um3','brand_id':'um4'})\n",
    "\n",
    "temp"
   ],
   "id": "de92d5b84a405a49",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          user_id  merchant_id  um2  um3  um4\n",
       "0               1          471    1    1    1\n",
       "1               1          739    1    1    1\n",
       "2               1          925    1    1    1\n",
       "3               1         1019    1    1    1\n",
       "4               1         1156    1    1    1\n",
       "...           ...          ...  ...  ...  ...\n",
       "14058661   424170         1082    1    1    1\n",
       "14058662   424170         3469    1    1    1\n",
       "14058663   424170         3736    7    2    1\n",
       "14058664   424170         4268    3    1    1\n",
       "14058665   424170         4963    1    1    1\n",
       "\n",
       "[14058666 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>um2</th>\n",
       "      <th>um3</th>\n",
       "      <th>um4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>471</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>739</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>925</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1019</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1156</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058661</th>\n",
       "      <td>424170</td>\n",
       "      <td>1082</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058662</th>\n",
       "      <td>424170</td>\n",
       "      <td>3469</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058663</th>\n",
       "      <td>424170</td>\n",
       "      <td>3736</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058664</th>\n",
       "      <td>424170</td>\n",
       "      <td>4268</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058665</th>\n",
       "      <td>424170</td>\n",
       "      <td>4963</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14058666 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:28.095818Z",
     "start_time": "2024-09-21T02:40:25.301799Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on=['user_id','merchant_id'],how='left')",
   "id": "b8bb631bb509d403",
   "outputs": [],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:51.295889Z",
     "start_time": "2024-09-21T02:40:30.601763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计操作类型为0，1，2，3的个数\n",
    "temp = groups['action_type'].value_counts().unstack().reset_index().rename(columns={0:'um5', 1:'um6', 2:'um7', 3:'um8'})\n",
    "\n",
    "temp"
   ],
   "id": "be9f377fe0b7115c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "action_type  user_id  merchant_id   um5  um6  um7  um8\n",
       "0                  1          471   1.0  NaN  NaN  NaN\n",
       "1                  1          739   1.0  NaN  NaN  NaN\n",
       "2                  1          925   3.0  NaN  1.0  NaN\n",
       "3                  1         1019  10.0  NaN  4.0  NaN\n",
       "4                  1         1156   1.0  NaN  NaN  NaN\n",
       "...              ...          ...   ...  ...  ...  ...\n",
       "14058661      424170         1082   1.0  NaN  NaN  NaN\n",
       "14058662      424170         3469   1.0  NaN  NaN  NaN\n",
       "14058663      424170         3736  10.0  NaN  NaN  NaN\n",
       "14058664      424170         4268  24.0  NaN  1.0  NaN\n",
       "14058665      424170         4963   1.0  NaN  NaN  NaN\n",
       "\n",
       "[14058666 rows x 6 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>action_type</th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>um5</th>\n",
       "      <th>um6</th>\n",
       "      <th>um7</th>\n",
       "      <th>um8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>471</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>739</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>925</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1019</td>\n",
       "      <td>10.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1156</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058661</th>\n",
       "      <td>424170</td>\n",
       "      <td>1082</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058662</th>\n",
       "      <td>424170</td>\n",
       "      <td>3469</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058663</th>\n",
       "      <td>424170</td>\n",
       "      <td>3736</td>\n",
       "      <td>10.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058664</th>\n",
       "      <td>424170</td>\n",
       "      <td>4268</td>\n",
       "      <td>24.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058665</th>\n",
       "      <td>424170</td>\n",
       "      <td>4963</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14058666 rows × 6 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:54.170259Z",
     "start_time": "2024-09-21T02:40:51.296889Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp,on=['user_id','merchant_id'],how='left')",
   "id": "63c157919e332de6",
   "outputs": [],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:56.033657Z",
     "start_time": "2024-09-21T02:40:54.171264Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计时间间隔\n",
    "temp = groups['time_stamp'].agg([('buy_far_time','min'),('buy_late_time','max')]).reset_index()\n",
    "temp['um9'] = (temp['buy_late_time'] - temp['buy_far_time']).dt.days\n",
    "temp"
   ],
   "id": "9fdd83a1081fab3d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          user_id  merchant_id buy_far_time buy_late_time  um9\n",
       "0               1          471   2020-11-11    2020-11-11    0\n",
       "1               1          739   2020-10-18    2020-10-18    0\n",
       "2               1          925   2020-10-11    2020-10-11    0\n",
       "3               1         1019   2020-11-11    2020-11-11    0\n",
       "4               1         1156   2020-11-11    2020-11-11    0\n",
       "...           ...          ...          ...           ...  ...\n",
       "14058661   424170         1082   2020-11-08    2020-11-08    0\n",
       "14058662   424170         3469   2020-11-05    2020-11-05    0\n",
       "14058663   424170         3736   2020-11-11    2020-11-11    0\n",
       "14058664   424170         4268   2020-11-05    2020-11-11    6\n",
       "14058665   424170         4963   2020-11-07    2020-11-07    0\n",
       "\n",
       "[14058666 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>buy_far_time</th>\n",
       "      <th>buy_late_time</th>\n",
       "      <th>um9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>471</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>739</td>\n",
       "      <td>2020-10-18</td>\n",
       "      <td>2020-10-18</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>925</td>\n",
       "      <td>2020-10-11</td>\n",
       "      <td>2020-10-11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1019</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1156</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058661</th>\n",
       "      <td>424170</td>\n",
       "      <td>1082</td>\n",
       "      <td>2020-11-08</td>\n",
       "      <td>2020-11-08</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058662</th>\n",
       "      <td>424170</td>\n",
       "      <td>3469</td>\n",
       "      <td>2020-11-05</td>\n",
       "      <td>2020-11-05</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058663</th>\n",
       "      <td>424170</td>\n",
       "      <td>3736</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058664</th>\n",
       "      <td>424170</td>\n",
       "      <td>4268</td>\n",
       "      <td>2020-11-05</td>\n",
       "      <td>2020-11-11</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14058665</th>\n",
       "      <td>424170</td>\n",
       "      <td>4963</td>\n",
       "      <td>2020-11-07</td>\n",
       "      <td>2020-11-07</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14058666 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.011916Z",
     "start_time": "2024-09-21T02:40:56.034655Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.merge(data,temp[['user_id','merchant_id','um9']],on=['user_id','merchant_id'],how='left')",
   "id": "a9a995e39c66eaa0",
   "outputs": [],
   "execution_count": 50
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "购买点击比",
   "id": "f019ce6c90a85858"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.027548Z",
     "start_time": "2024-09-21T02:40:59.014911Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 用户购买点击比 u9购买  u7点击 \n",
    "data['r1'] = data['u9']/data['u7']"
   ],
   "id": "3bcd60ef7339a451",
   "outputs": [],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.037313Z",
     "start_time": "2024-09-21T02:40:59.029547Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 商家购买点击比 m8 m6\n",
    "data['r2'] = data['m8']/data['m6']"
   ],
   "id": "435aab5761e78075",
   "outputs": [],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.043890Z",
     "start_time": "2024-09-21T02:40:59.038312Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不同用户不同商家购买点击比 um7 um5 \n",
    "data['r3'] = data['um7']/data['um5']"
   ],
   "id": "4230f5c573fdcf22",
   "outputs": [],
   "execution_count": 53
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "one_hot编码",
   "id": "203033135cb03852"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.069351Z",
     "start_time": "2024-09-21T02:40:59.044888Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 年龄\n",
    "temp = pd.get_dummies(data['age_range'], prefix='age')\n",
    "\n",
    "temp "
   ],
   "id": "8152fd91935a3ea8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        age_0  age_1  age_2  age_3  age_4  age_5  age_6  age_7  age_8\n",
       "0       False  False  False  False  False  False   True  False  False\n",
       "1       False  False  False  False  False   True  False  False  False\n",
       "2       False  False  False  False  False   True  False  False  False\n",
       "3       False  False  False  False  False   True  False  False  False\n",
       "4       False  False  False  False  False   True  False  False  False\n",
       "...       ...    ...    ...    ...    ...    ...    ...    ...    ...\n",
       "522336   True  False  False  False  False  False  False  False  False\n",
       "522337   True  False  False  False  False  False  False  False  False\n",
       "522338   True  False  False  False  False  False  False  False  False\n",
       "522339  False  False  False  False  False  False   True  False  False\n",
       "522340  False  False  False   True  False  False  False  False  False\n",
       "\n",
       "[522341 rows x 9 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age_0</th>\n",
       "      <th>age_1</th>\n",
       "      <th>age_2</th>\n",
       "      <th>age_3</th>\n",
       "      <th>age_4</th>\n",
       "      <th>age_5</th>\n",
       "      <th>age_6</th>\n",
       "      <th>age_7</th>\n",
       "      <th>age_8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522336</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522337</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522338</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522339</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522340</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>522341 rows × 9 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.185418Z",
     "start_time": "2024-09-21T02:40:59.070364Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.concat([data,temp],axis=1)",
   "id": "c311d45b28b9a59e",
   "outputs": [],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.203688Z",
     "start_time": "2024-09-21T02:40:59.186421Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 性别\n",
    "temp = pd.get_dummies(data['gender'], prefix='gender')\n",
    "\n",
    "temp "
   ],
   "id": "9340f31a35c0cf52",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        gender_0  gender_1  gender_2\n",
       "0          False      True     False\n",
       "1           True     False     False\n",
       "2           True     False     False\n",
       "3           True     False     False\n",
       "4           True     False     False\n",
       "...          ...       ...       ...\n",
       "522336     False      True     False\n",
       "522337     False      True     False\n",
       "522338     False     False      True\n",
       "522339     False      True     False\n",
       "522340     False      True     False\n",
       "\n",
       "[522341 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>gender_0</th>\n",
       "      <th>gender_1</th>\n",
       "      <th>gender_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522336</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522337</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522338</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522339</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522340</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>522341 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.254812Z",
     "start_time": "2024-09-21T02:40:59.204689Z"
    }
   },
   "cell_type": "code",
   "source": "data = pd.concat([data,temp],axis=1)",
   "id": "b0d9a79e84e50375",
   "outputs": [],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.318307Z",
     "start_time": "2024-09-21T02:40:59.255811Z"
    }
   },
   "cell_type": "code",
   "source": "data.drop(columns=['age_range','gender'],inplace=True)",
   "id": "151e40a1827d273b",
   "outputs": [],
   "execution_count": 58
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "分割数据",
   "id": "18f5cf9dba33c8a5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.501446Z",
     "start_time": "2024-09-21T02:40:59.319362Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train = data[data['origin']=='train'].drop(['origin'],axis=1)\n",
    "test = data[data['origin']=='test'].drop(['label','origin'],axis=1)"
   ],
   "id": "60c2e78d5924fc12",
   "outputs": [],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.531288Z",
     "start_time": "2024-09-21T02:40:59.503441Z"
    }
   },
   "cell_type": "code",
   "source": "X,Y = train.drop(['label'],axis=1),train['label'] ",
   "id": "51a6b3cdb05f20aa",
   "outputs": [],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.717086Z",
     "start_time": "2024-09-21T02:40:59.532290Z"
    }
   },
   "cell_type": "code",
   "source": "from sklearn.model_selection import train_test_split",
   "id": "903aeefc931d0e5a",
   "outputs": [],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:40:59.797677Z",
     "start_time": "2024-09-21T02:40:59.718091Z"
    }
   },
   "cell_type": "code",
   "source": "train_x,valid_x,train_y,valid_y = train_test_split(X,Y,test_size=0.2)",
   "id": "e4634c5833619f34",
   "outputs": [],
   "execution_count": 62
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "建模预测（LightGBM）\n",
    "为什么选择LightGBM\n",
    "非常快的训练速度与高效\n",
    "非常低的内存消耗\n",
    "非常高的准确率\n",
    "并发和支持GPU加速\n",
    "能处理庞大体量的数据\n",
    "能直接处理缺失值"
   ],
   "id": "a5f9e1e173a4c9f7"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "建模预测",
   "id": "6ea6d4bd84c730b9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:41:00.832216Z",
     "start_time": "2024-09-21T02:40:59.798676Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from lightgbm import LGBMClassifier\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score"
   ],
   "id": "b443075602209e94",
   "outputs": [],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:41:00.835471Z",
     "start_time": "2024-09-21T02:41:00.833216Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model = LGBMClassifier(\n",
    "    num_leaves=10,\n",
    "    learning_rate=0.05,\n",
    "    n_estimators=1000,\n",
    "    subsample=0.8,\n",
    ")"
   ],
   "id": "4422277def4737db",
   "outputs": [],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:41:58.037549Z",
     "start_time": "2024-09-21T02:41:44.446803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model.fit(\n",
    "    train_x,train_y,\n",
    "    eval_set=[(train_x,train_y), (valid_x,valid_y)],\n",
    "    eval_metric='auc',\n",
    "    # verbose=50, \n",
    "    # early_stopping_rounds=30 #早停法，如果auc在30epoch没有进步就stop\n",
    ")"
   ],
   "id": "d979f760150cb072",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Info] Number of positive: 12811, number of negative: 195880\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.015176 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6176\n",
      "[LightGBM] [Info] Number of data points in the train set: 208691, number of used features: 45\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061387 -> initscore=-2.727198\n",
      "[LightGBM] [Info] Start training from score -2.727198\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "LGBMClassifier(learning_rate=0.05, n_estimators=1000, num_leaves=10,\n",
       "               subsample=0.8)"
      ],
      "text/html": [
       "<style>#sk-container-id-1 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-1 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-1 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-1 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-1 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-1 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LGBMClassifier(learning_rate=0.05, n_estimators=1000, num_leaves=10,\n",
       "               subsample=0.8)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;LGBMClassifier<span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>LGBMClassifier(learning_rate=0.05, n_estimators=1000, num_leaves=10,\n",
       "               subsample=0.8)</pre></div> </div></div></div></div>"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:42:22.788Z",
     "start_time": "2024-09-21T02:42:19.315418Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('accuracy：',accuracy_score(Y,model.predict(X)))\n",
    "print('roc_auc：',roc_auc_score(Y,model.predict_proba(X)[:,1]))"
   ],
   "id": "52989654bd370d71",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy： 0.9392365370461236\n",
      "roc_auc： 0.7537233664699429\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-21T02:47:09.734371Z",
     "start_time": "2024-09-21T02:47:08.179221Z"
    }
   },
   "cell_type": "code",
   "source": [
    "prob = model.predict_proba(test)[:,1]\n",
    "\n",
    "submission = pd.DataFrame()\n",
    "submission[['user_id','merchant_id']] = test[['user_id','merchant_id']]\n",
    "submission['prob'] = prob\n",
    "submission.to_csv('D:/桌面/天猫复购预测/data/prediction.csv',index=False) "
   ],
   "id": "ca16bb4d39fe5789",
   "outputs": [],
   "execution_count": 69
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
